package com.eightywork.life.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import android.widget.Toast;

import cn.jpush.android.api.JPushInterface;

import com.eightywork.life.R;
import com.eightywork.life.service.CacheService;
import com.umeng.analytics.MobclickAgent;

public class BaseActivity extends FragmentActivity {
	public BaseActivity context = this;
	protected ProgressDialog progressDialog;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
	}

//	@Override
//	public void onResume() {		
//		super.onResume();
//		JPushInterface.onResume(this);
//	    MobclickAgent.onResume(this);
//	}
//	
//	@Override
//	public void onPause() {
//	    super.onPause();
//		JPushInterface.onPause(this);
//	    MobclickAgent.onPause(this);
//	}


	public void showProgressDialog(String message) {
		if (progressDialog == null) {
			progressDialog = new ProgressDialog(context);
		}
		if (message != null) {
			progressDialog.setMessage(message);
		}
		progressDialog.show();
	}

	public void hideProgressDialog() {
		progressDialog.dismiss();
	}

	public void showMessage(final String message) {
		runOnUiThread(new Runnable() {
			@Override
			public void run() {
				Toast toast = Toast.makeText(context, message,
						Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
			}
		});
	}

	public void showAlert(String title, String message,
			DialogInterface.OnClickListener listener) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(title);
		builder.setMessage(message);
		builder.setPositiveButton("确定", listener);
		builder.show();
	}
	
	public void hideKeyBoard(Activity activity) {
		InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);  
		imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
	}

	public Dialog initDialog(int layout) {
		Dialog dialog = new Dialog(context, R.style.dialog);
		View viewToLoad = LayoutInflater.from(context).inflate(layout, null);
		dialog.setContentView(viewToLoad);
		dialog.setCanceledOnTouchOutside(true);
		return dialog;
	}

	public void initHeader(String text) {
		TextView titleView = (TextView) findViewById(R.id.header_title);
		titleView.setText(text);
	}

	public boolean checkLogin() {
		if (!CacheService.isLogined()) {
			Intent intent = new Intent();
			intent.setClass(context, RegFirstActivity.class);
			context.startActivity(intent);
			return false;
		}
		return true;
	}

}
